package com.xgg.taiwuinfo.filter.fc;

import android.content.Context;
import android.util.Log;

import com.xgg.taiwuinfo.filter.FC_CompareSelect;
import com.xgg.taiwuinfo.filter.FilterComponent;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FCGridOccuppyCompareSelect extends FC_CompareSelect {

    public FCGridOccuppyCompareSelect(JSONObject obj)
    {
        mTypeId = FilterComponent.FT_GRIDOCCUPY_COMP_SELECT;
        if (obj != null) {
            try {
                ReadCompare = obj.getInt("c");
                ReadValue = obj.getInt("v");
            } catch (Exception e) {
                Log.d("LoadJson", e.getLocalizedMessage());
                ReadCompare = FC_EQUAL;
                ReadValue = 1;
            }
        }
        else {
            ReadCompare = FC_EQUAL;
            ReadValue = 1;
        }
        TempValue = ReadValue;
        TempCompare = ReadCompare;

        if (mValueMap == null) {
            mValueMap = new ArrayList<>();
            Map<String, String> map;

            map = new HashMap<>();
            map.put("f", "1");
            map.put("v", "1");
            mValueMap.add(map);
            map = new HashMap<>();
            map.put("f", "2");
            map.put("v", "2");
            mValueMap.add(map);
            map = new HashMap<>();
            map.put("f", "3");
            map.put("v", "3");
            mValueMap.add(map);
        }
        FieldStr = "功法占用格子";
    }

    @Override
    public String FilterGongfa(Gongfa gongfa){
        String fmt = "占用 %d 格";
        switch (ReadCompare)
        {
            case FC_GREATER:
                if (gongfa.getGridOccupy() > ReadValue)
                    return String.format(fmt , gongfa.getGridOccupy());
                else
                    return null;
            case FC_GREATERCONTAIN:
                if (gongfa.getGridOccupy() >= ReadValue)
                    return String.format(fmt , gongfa.getGridOccupy());
                else
                    return null;
            case FC_EQUAL:
                if (gongfa.getGridOccupy() == ReadValue)
                    return String.format(fmt , gongfa.getGridOccupy());
                else
                    return null;
            case FC_SMALLERCONTAIN:
                if (gongfa.getGridOccupy() <= ReadValue)
                    return String.format(fmt , gongfa.getGridOccupy());
                else
                    return null;
            case FC_SMALLER:
                if (gongfa.getGridOccupy() < ReadValue)
                    return String.format(fmt , gongfa.getGridOccupy());
                else
                    return null;
            default:
                return null;
        }
    }

    @Override
    public List<Map<String, String>> getValueMap(Context ctx)
    {
        return mValueMap;
    }

    @Override
    public List<Map<String, String>> getFieldString(Context ctx){
        List<Map<String, String>> mFieldMap = new ArrayList<>();

        Map<String, String> map;
        map = new HashMap<>();
        map.put("f", FieldStr);
        map.put("v",   "0");
        mFieldMap.add(map);
        return mFieldMap;
    }
}
